package com.hyt.it.ogt.kq.service.bm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.service.bm.feign.kw.KwClient;
import com.hyt.it.ogt.kq.service.bm.feign.kw.model.ArrageStudent;
import com.hyt.it.ogt.kq.service.bm.mapper.ProjectSubjectMapper;
import com.hyt.it.ogt.kq.service.bm.mapper.SubjectExamMapper;
import com.hyt.it.ogt.kq.service.bm.model.entity.Project;
import com.hyt.it.ogt.kq.service.bm.model.entity.ProjectSubject;
import com.hyt.it.ogt.kq.service.bm.model.entity.SubjectExam;
import com.hyt.it.ogt.kq.service.bm.model.entity.SubjectExamHistory;
import com.hyt.it.ogt.kq.service.bm.model.vo.StudentParam;
import com.hyt.it.ogt.kq.service.bm.model.vo.StudentVo;
import com.hyt.it.ogt.kq.service.bm.service.IProjectService;
import com.hyt.it.ogt.kq.service.bm.service.IStudentInfoService;
import com.hyt.it.ogt.kq.service.bm.service.IStudentSubjectService;
import com.hyt.it.ogt.kq.service.bm.service.ISubjectExamHistoryService;
import com.hyt.it.ogt.kq.service.bm.service.ISubjectExamService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liteng
 * @since 2020-06-03
 */
@Service
public class SubjectExamServiceImpl extends BaseServiceImpl<SubjectExamMapper, SubjectExam>
		implements ISubjectExamService {

	@Autowired
	ISubjectExamService iSubjectExamService;
	@Autowired
	IProjectService iProjectService;

	@Autowired
	IStudentInfoService iStudentInfoService;

	@Autowired
	IStudentSubjectService iStudentSubjectService;

	@Autowired
	ProjectSubjectMapper projectSubjectMapper;

	@Autowired
	ISubjectExamHistoryService iSubjectExamHistoryService;
	@Autowired
	KwClient kwClient;

	@Transactional(rollbackFor = Exception.class)
	public ApiResponse saveSubjectExam(SubjectExam subjectExam, List<SubjectExam> examList) throws Exception {
		ApiResponse api = new ApiResponse();
		try {
			StudentParam studentParam = new StudentParam();
			studentParam.setSubjectId(subjectExam.getSubjectId());
			studentParam.setProjectId(subjectExam.getProjectId());
			Project project = iProjectService.getById(subjectExam.getProjectId());
			if(project.getIsVerify() !=null && project.getIsVerify() == 1) {
				studentParam.setArrageState("1");
			}
			ProjectSubject projectSubject = projectSubjectMapper.queryBySubjectId(subjectExam.getProjectId(), subjectExam.getSubjectId());
			if (projectSubject.getIsPay() != null && projectSubject.getIsPay().intValue() == 1) {
				studentParam.setPayState("1");
			}
			QueryWrapper<SubjectExam> wrapper = new QueryWrapper<SubjectExam>();
			wrapper.eq("project_id", subjectExam.getProjectId());
			wrapper.eq("subject_id", subjectExam.getSubjectId());
			SubjectExam historyExam = iSubjectExamService.getOne(wrapper);
			if (examList.isEmpty()) {
				QueryWrapper<SubjectExamHistory> historyQueryWrapper
					= new QueryWrapper<SubjectExamHistory>();
				historyQueryWrapper.eq("project_id", subjectExam.getProjectId());
				historyQueryWrapper.eq("exam_id", subjectExam.getExamId());
				historyQueryWrapper.eq("subject_id", subjectExam.getSubjectId());
				List<SubjectExamHistory> historyList =
						iSubjectExamHistoryService.list(historyQueryWrapper);
				if (historyList.isEmpty()) {
					SubjectExamHistory history = new SubjectExamHistory();
					history.setExamId(subjectExam.getExamId());
					history.setExamName(subjectExam.getExamName());
					history.setSubjectId(subjectExam.getSubjectId());
					history.setIsAuto(subjectExam.getIsAuto());
					history.setProjectId(subjectExam.getProjectId());
					iSubjectExamHistoryService.save(history);
				}
			}
			List<StudentVo> studentList = iStudentInfoService.queryStudentList(studentParam, new ArrayList<String>(),0L, 0L);
			if (studentList != null && studentList.size() > 0) {
				String[] studentId = new String[studentList.size()];
				for (int i = 0; i < studentList.size(); i++) {
					studentId[i] = studentList.get(i).getStudentId();
				}
				ArrageStudent student = new ArrageStudent();
				student.setCandidateIds(studentId);
				student.setEnrollId(subjectExam.getProjectId());
				student.setExamId(subjectExam.getExamId());
				student.setSubjectId(subjectExam.getSubjectId());
				api = kwClient.arrageStudent(student);
			}
			if (api.getCode() != null && api.getCode().intValue() == 16239) {
				throw new Exception("16239");
			} else {
				if (studentList != null && studentList.size() > 0) {
					iSubjectExamService.saveOrUpdate(subjectExam);
					return ApiResponse.
							builder().
							code(Constant.RESPONSE_CODE_SUCCESS).
							data(subjectExam).build();
				} else {
					return ApiResponse.
							builder().
							code(Constant.RESPONSE_CODE_14055).
							data(subjectExam).build();
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

	}

}
